package umputun.jutw.jabber;
/*
 Copyright 2009 Umputun

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
*/

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import umputun.jutw.utils.logging.ULogger;


public final class JabberClient {

	public static class Builder {

		public JabberClient build() {return new JabberClient(this);}

		private Builder(String addr) {this.addr = addr;}

		public Builder setPort(int port) {this.port = port; return this;}
		public Builder setUser(String user) {this.user = user; return this;}
		public Builder setPassword(String password) {this.password = password; return this;}
		public Builder setService(String service) {this.service = service; return this;}

		private final String addr;
		private int port = 5222;
		private String user, password, service;
	}

	public static Builder newBuilder(String addr) {return new Builder(addr);}

	private JabberClient(Builder bld) {
		this.pr = bld;
		log.info("jaber init. server="+pr.addr + " port=" + pr.port + " service=" + pr.service + " user="+pr.user);
		conn_config = (pr.service != null) ?
			new ConnectionConfiguration(pr.addr, pr.port, pr.service) : new ConnectionConfiguration(pr.addr, pr.port);
			log.fine("jabber client created");
	}


	public void setSelfSignedCertificateEnabled(boolean value) {
		conn_config.setSelfSignedCertificateEnabled(value);
		log.fine("setSelfSignedCertificateEnabled=" + value);
	}
	public void setNotMatchingDomainCheckEnabled(boolean value) {
		conn_config.setNotMatchingDomainCheckEnabled(value);
		log.fine("setNotMatchingDomainCheckEnabled=" + value);
	}
	public void setSASLAuthenticationEnabled(boolean value) {
		conn_config.setSASLAuthenticationEnabled(value);
		log.fine("setSASLAuthenticationEnabled=" + value);
	}

	public void setSASLPlain() {
		SASLAuthentication.supportSASLMechanism("PLAIN");
	}

	public void setSecurityMode(boolean value) {
		conn_config.setSecurityMode(value ? ConnectionConfiguration.SecurityMode.enabled :
			ConnectionConfiguration.SecurityMode.disabled);
		log.fine("setSecurityMode=" + value);
	}
	public void setReconnectionAllowed(boolean value) {
		conn_config.setReconnectionAllowed(value);
		log.fine("setReconnectionAllowed=" + value);
	}
	public void setCompressionEnabled(boolean value) {
		conn_config.setCompressionEnabled(value);
		log.fine("setReconnectionAllowed=" + value);
	}


	public ConnectionConfiguration getJabberconfig() {
		return conn_config;
	}

	public XMPPConnection connect() throws XMPPException {
		log.finest("init connect");
		conn = new XMPPConnection(conn_config);
		conn.connect();
		log.finest("connected");
		conn.login(pr.user, pr.password, "JUTW");
		log.finest("logged in as " + pr.user);
		log.fine("jabber fully connected");
		return conn;
	}

	public void disconnect() {
		if (conn != null)
			conn.disconnect();
	}

	public void sendMessage(String to_jid, String msg) throws XMPPException {
		if (conn == null) throw new XMPPException("not connected");

		ChatManager chatmanager = conn.getChatManager();
		Chat newChat = chatmanager.createChat(to_jid, new MessageListener() {
			public void processMessage(Chat chat, Message message) {
				log.finest("received message: " + message);
			}
		});
		newChat.sendMessage(msg);
		log.fine("message sent to jabber: " + msg);
	}


	private final Builder pr;
	private final ConnectionConfiguration conn_config;
	private XMPPConnection conn;
	private static final ULogger log = ULogger.getLogger();
}

